const db = wx.cloud.database()
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
	  schools:[],
	  pickerdata:{all:"全国"},
	  skip:0,
	  users:[]
  },
	getPicker(e){
		console.log(e.detail)
		this.setData({
			skip:0
		})
		let pickerdata = e.detail
		//全部人
		if (pickerdata.all != undefined){
			db.collection('users').limit(18).skip(this.data.skip).get().then(res=>{
				let users = res.data
				this.setData({
					skip: 18,
					users:users,
					pickerdata: e.detail
				})
			})
			return
		}
		//province
		if (pickerdata.province != undefined) {
			wx.cloud.callFunction({
				name:"getCities",
				data: { province: pickerdata.province}
			}).then(res=>{
				// console.log(res.result.data[0].cities)
				let cities = res.result.data[0].cities
				let schools = []
				for(let index in cities){
					schools = schools.concat(cities[index].universities)
				}
				console.log(schools)
				db.collection('users').where({
					school:_.in(schools)
				}).limit(18).skip(this.data.skip).get().then(eres => {
					console.log(eres)
					let users = eres.data
					this.setData({
						skip: 18,
						users: users,
						schools:schools
					})
				})
				return
			})
		}
		//city
		if (pickerdata.city != undefined) {
			db.collection('school').where({
				cities: { city_name: pickerdata.city}
			}).get().then(res=>{
				console.log(res)
				let cities = res.data[0].cities
				let schools = []
				for(let index in cities){
					if (cities[index].city_name == pickerdata.city){
						schools = schools.concat(cities[index].universities)
						break
					}
				}
				console.log(schools)
				db.collection('users').where({
					school:_.in(schools)
				}).limit(18).skip(this.data.skip).get().then(eres => {
					console.log(eres)
					let users = eres.data
					this.setData({
						skip: 18,
						users: users,
						schools:schools
					})
					return
				})
			})
		}
		//school
		if (pickerdata.school != undefined) {
			db.collection('users').where({
				school: pickerdata.school
			}).limit(18).skip(this.data.skip).get().then(res => {
				let users = res.data
				this.setData({
					skip: 18,
					users: users,
					schools: this.data.schools.concat(pickerdata.school)
				})
			})
			return
		}
	},
	/**
 * 页面上拉触底事件的处理函数
 */
	onReachBottom: function () {
		if (this.data.pickerdata.all != undefined) {
			//all
			db.collection('users').limit(18).skip(this.data.skip).get().then(res => {
				let users = this.data.users.concat(res.data)
				this.setData({
					skip:this.data.skip+18,
					users: users
				})
			})
			return
		}else{
			//other
			db.collection('users').where({
				school:_.in(this.data.schools)
			}).limit(18).skip(this.data.skip).get().then(res => {
				let users = this.data.users.concat(res.data)
				this.setData({
					skip: this.data.skip + 18,
					users: users
				})
			})
		}
	},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
	  if (this.data.pickerdata.all != undefined) {
		  db.collection('users').limit(18).skip(this.data.skip).get().then(res => {
			  let users = this.data.users.concat(res.data)
			  this.setData({
				  skip:18,
				  users: users
			  })
		  })
		  return
	  }
  }
})